﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Giaiphongmatbang.BLL;
using Giaiphongmatbang.DataModel;

namespace Giaiphongmatbang.UserControl
{
    public partial class getFunctions : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        public void BindData()
        {
            User user = (User)Session["User"];
            if (user == null)
            {
                user = new User();
                user.UserName = "guest";
            }
            StringBuilder nav = new StringBuilder("<ul><li><a href='/Home.aspx'><img src='/Images/redhat-home.png' alt='' title='Trang chủ' style='margin-top:2px'/></a></li>");
            IEnumerable<Function> listFunction = new FunctionRepository().GetByUser(user.UserName);
            foreach (var item in listFunction.Where(p => p.ParentID == 0))
            {
                nav.AppendLine("<li><a href='" + item.Url + "' id='" + item.FunctionID + "'>" + item.FunctionName + "</a>");
                nav.AppendLine("<ul>");
                foreach (var child in listFunction.Where(p => p.ParentID == item.FunctionID))
                {
                    nav.AppendLine("<li><a href='" + child.Url + "' id='" + child.FunctionID + "'>" + child.FunctionName + "</a></li>");
                }
                nav.AppendLine("</ul></li>");
            }
            nav.AppendLine("</ul>");
            ltrMainNav.Text = nav.ToString();           
        }

    }
}